<?php
/**
 * 收货地址控制器
 * @author e5
 *
 */
class AddressController extends CommonController{
	private $db;
	public function __init() {
		parent::__init();
		$this->checkLogin();
		$this->db = K("Address");
	}
	public function index() {
		//获得全部分类
		$cateData = K("Category")->all();
		//获得顶级分类
		$topCate = array();
		foreach ($cateData as $v){
			if ($v['pid'] == 0){
				$topCate[] = $v;
			}
		}
		$this->assign("topCate",$topCate);
		$this->assign("cateData",$cateData);
		$uid = session("uid");
		$arData = $this->db->where("vshop_user_uid={$uid}")->all();
		foreach ($arData as $k=>$arv){
			$arData[$k]['area'] = $arv['ararea'].$arv['detailarea'];
		}
		$this->assign("arData",$arData);
		$this->dis("address");
	}
	//添加收货地址
	public function add(){
		if (IS_POST){
			$arid = $this->db->addArea();
			if (!$arid) $this->error($this->db->error);
			if (Q("post.arisdefault",0,"intval") == 1){
				$this->db->where("arid<>{$arid}")->update(array("arisdefault"=>0));
			}
			$this->success("添加收货地址成功",U("index"));
		}
	}
	public function delArea(){
		$arid = Q("get.arid",0,"intval");
		$uid = session("uid");
		if (!$this->db->where("vshop_user_uid={$uid} AND arid={$arid}")->delete()) $this->error("删除地址失败");
		$this->success("删除收货地址成功",U("index"));
	}
}